//
//  APIManager.h
//  YYYNetWorkDemo
//
//  Created by yyy on 2016/12/6.
//  Copyright © 2016年 yyy. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, APIRequestMethod) {
    kAPIRequestMethodGET = 0,
    kAPIRequestMethodPOST,
    kAPIRequestMethodHEAD,
    kAPIRequestMethodPUT,
    kAPIRequestMethodDELETE,
    kAPIRequestMethodPATCH,
};

///  Request serializer type.
typedef NS_ENUM(NSInteger, APIRequestSerializerType) {
    kAPIRequestSerializerTypeHTTP = 0,
    kAPIRequestSerializerTypeJSON,
};

typedef NS_ENUM(NSInteger, APIResponseSerializerType) {
    /// NSData type
        kAPIResponseSerializerTypeHTTP,
    /// JSON object type
        kAPIResponseSerializerTypeJSON,
    /// NSXMLParser type
        kAPIResponseSerializerTypeTypeXMLParser,
};

///  Request priority
typedef NS_ENUM(NSInteger, APIRequestPriority) {
    kAPIRequestPriorityLow = -4L,
    kAPIRequestPriorityDefault = 0,
    kAPIRequestPriorityHigh = 4,
};

typedef NS_ENUM(NSUInteger, APIRequestPolicy) {
    /** 优先先读取缓存,若读取成功,不再发起请求,反之读远程数据 */
        kAPIRequestPolicyReturnCacheDataElseReloadRemoteData,
    /** 忽略缓存,仅读远程数据 */
        kAPIRequestPolicyReloadRemoteDataIgnoringCacheData,
    /** 优先先读取缓存,若读取成功,先执行回调逻辑,再读远程数据,反之读远程数据 */
        kAPIRequestPolicyReturnCacheDataThenReloadRemoteData,
    /** 优先读取远程数据,若读取失败,读取缓存 */
        kAPIRequestPolicyReloadRemoteDataElseReturnCacheData,
};
@class URLResponse;
@protocol APIManager <NSObject>

@required
- (NSString *)APIPath; /**< 路径 */

@optional
- (APIRequestMethod)APIMethod;
- (NSString *)APIScheme; //default https
- (APIRequestSerializerType)APIRequestSerializerType;
- (APIResponseSerializerType)APIResponseSerializerType;
- (APIRequestPriority)APIRequestPriority;
- (NSString *)APIBaseUrl;                                /**< 基础链接 */
- (NSTimeInterval)APIHTTPRequestTimeoutInterval;         /**< 请求超时时间 */
- (NSDictionary<NSString *, NSString *> *)APIHTTPHeader; /**< 自定义添加请求头 */

#pragma mark - 缓存
- (BOOL)APIShouldCache;                  /**< 是否缓存，优先级最高，返回NO所有缓存都关闭 */
- (BOOL)APIShouldCacheInFile;            /**< 根据文件缓存 */
- (NSTimeInterval)APIFileCacheTimeOut;   /**< 文件缓存时间 */
- (NSTimeInterval)APIMemoryCacheTimeOut; /**< 内存缓存时间 */
- (NSString *)APICacheWithVersion;       /**< 根据版本号缓存 */
- (NSString *)APICacheKey;

#pragma mark end -

/**
 发起请求前，对参数进行处理

 @param params 请求参数
 @return 处理后的参数
 */
- (id)APIReformParams:(id)params;

/**
 对返回的数据进行重组，可在这边直接转换成对应的模型

 @param responseObject 返回的数据
 @return 重组后的数据
 */
- (id)APIReformResponseObject:(id)responseObject;

/**
 request 发起请求前的处理
 
 @param request request
 @return 处理后的 request
 */
- (NSMutableURLRequest *)APIReformRequest:(NSMutableURLRequest *)request;

// 拦截器方法
- (BOOL)shouldCallAPIWithParams:(id)params;
- (void)afterCallAPIWithParams:(id)params;

- (BOOL)beforePerformCallBackWithResponse:(URLResponse *)response;
- (void)afterPerformCallBackWithResponse:(URLResponse *)response;
- (BOOL)beforePerformSuccessWithResponse:(URLResponse *)response;
- (void)afterPerformSuccessWithResponse:(URLResponse *)response;
- (BOOL)beforePerformFailWithResponse:(URLResponse *)response;
- (void)afterPerformFailWithResponse:(URLResponse *)response;

/**
 验证请求参数，正确返回nil，错误返回对应的error

 @param params 请求参数
 @return 正确返回nil，错误返回对应的error
 */
- (NSError *)validateParameterData:(id)params;

- (NSError *)validateCallBackData:(id)data;
@end

NS_ASSUME_NONNULL_END
